<template>
  <div class="app-container">
    <el-table v-loading="loading" border :data="dataList">
      <el-table-column label="模板类型" align="center">
        <template slot-scope="scope">
          {{ getCarLabel(scope.row.shoppingCarEnums) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:template:edit']"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改合同模板对话框 -->
    <el-dialog title="修改合同模板" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板类型" prop="shoppingCarEnums">
          <el-select
            v-model="form.shoppingCarEnums"
            style="width: 100%"
            disabled
            placeholder="请选择合同模板类型"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="content">
          <Editors v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTemplate, updateTemplate } from "@/api/system/template";

export default {
  name: "Template",
  data() {
    return {
      loading: false, // 遮罩层
      dataList: [], // 合同模板表格数据
      open: false, // 是否显示弹出层
      form: {}, // 表单参数
      rules: {
        content: [{ required: true, message: "模板内容不能为空", trigger: "blur" }],
      }, // 表单校验
      typeList: [
        { value: "CLOUD_SERVER", label: "云服务器" },
        { value: "CLOUD_MARKET", label: "云市场" },
        { value: "GOV_CLOUD", label: "政务云" },
        { value: "CLOUD_SERVER_RENEW", label: "云服务器续费" },
        { value: "CLOUD_SERVER_CHANGE", label: "云服务器变更" },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同模板列表 */
    getList() {
      this.loading = true;
      listTemplate({})
        .then((res) => {
          this.dataList = res.rows;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 获取合同模板类型
    getCarLabel(val) {
      let row = this.typeList.find((i) => i.value === val);
      return row ? row.label : "";
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shoppingCarEnums: null,
        content: "",
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = Object.assign({}, row);
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateTemplate(this.form).then((res) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
